package com.lovo.demo;

import javax.swing.JOptionPane;

import com.lovo.util.ArrayUtil;

public class Admin {
	public static void adminMenu() {
		while(true){
			String input = JOptionPane.showInputDialog(null, "欢迎使用员工管理\n\n"
					+ "1.查看所有员工\n"
					+ "2.添加员工\n"
					+ "3.删除员工\n"
					+ "4.修改密码\n"
					+ "5.安全退出");
			if(input == null)
				input = "";
				
			switch(input){
				case "1": show();
					break;
				case "2": add(); 
					break;
				case "3": dele();
					break;
				case "4": updatePwd();
					break;
				case "5": ComSystemDemo.process();
					break;
				default: JOptionPane.showMessageDialog(null, "输入错误");
					break;
			}
		}
	}

	public static void updatePwd() {
		String password = JOptionPane.showInputDialog(null, "请输入密码");
		String[][] users = ComSystemDemo.getUsers();
		
		if(users[2][ComSystemDemo.getIndex()].equals(password)){
			String newPwd1 = JOptionPane.showInputDialog(null, "请输入新密码");
			String newPwd2 = JOptionPane.showInputDialog(null, "请确认新输入密码");
			
			if(newPwd1 != null && newPwd2 != null && !"".equals(newPwd1) && !"".equals(newPwd2)){
				if(newPwd1.equals(newPwd2)){
					users[2][ComSystemDemo.getIndex()] = newPwd2;
					JOptionPane.showMessageDialog(null, "修改成功");
				}else{
					JOptionPane.showMessageDialog(null, "修改失败！！两次密码不一致");
				}
			}else{
				JOptionPane.showMessageDialog(null, "修改失败！！密码不能为空");
			}
		}else{
			JOptionPane.showMessageDialog(null, "密码错误");
		}
	}

	public static void show() {
		String[][] users = ComSystemDemo.getUsers();
		String showUser = "  姓名    等级\n\n";
		String user = "";
		
		for (int i = 0; i < users[1].length; i++) {
			if("a".equals(users[0][i])){
				user = "管理员";
			}else{
				user = "员工";
			}
			showUser = showUser +"    "+ users[1][i] + "    " + user + "\n";
		}
		JOptionPane.showMessageDialog(null, showUser);
	}
	
	public static void add(){
		String name = JOptionPane.showInputDialog(null, "请输入员工姓名");
		String level = JOptionPane.showInputDialog(null, "请输入员工级别(a\\u)\n"
				+ "a.管理员\n"
				+ "u.普通员工\n");
		String[][] newUser = ComSystemDemo.getUsers();
		
		if(("a".equals(level) || "u".equals(level)) && (name != null && !"".equals(name))){
			newUser[0] = ArrayUtil.add(newUser[0], level);
			newUser[1] = ArrayUtil.add(newUser[1], name);
			newUser[2] = ArrayUtil.add(newUser[2], "666");
			JOptionPane.showMessageDialog(null, "添加成功");
		}else{
			JOptionPane.showMessageDialog(null, "输入错误");
		}
	}
	
	public static void dele(){
		String name = JOptionPane.showInputDialog(null, "请输入要删除的员工姓名");
		String[][] newUsers = ComSystemDemo.getUsers();
		int index = -1;
		boolean flag = false;
		
		for (int i = 0; i < newUsers[1].length; i++) {
			if(newUsers[1][i].equals(name)){
				index = i;
				flag = true;
				break;
			}
		}
		
		if(flag){
			newUsers[0] = ArrayUtil.delet(newUsers[0], index+1);
			newUsers[1] = ArrayUtil.delet(newUsers[1], index+1);
			newUsers[2] = ArrayUtil.delet(newUsers[2], index+1);
		}else{
			JOptionPane.showMessageDialog(null, "查无此人");
		}
	}	
}
